package com.Sing.Controller;

import com.Sing.Pojo.Type;
import com.Sing.Service.TypeService;
import com.Sing.Utils.Constant;
import com.fasterxml.jackson.databind.util.JSONPObject;
import com.mysql.cj.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

@Controller
public class TypeController {

    private TypeService typeService;
    @Autowired
    @Qualifier("typeServiceImpl")
    public void setTypeService(TypeService typeService) {
        this.typeService = typeService;
    }

    // 跳转到类型管理页面
    @RequestMapping("/typeIndex")
    public String typeIndexJump(){

        return "type/typeIndex";
    }

    /**
     * 获取类型表中的数据并返回给前端页面
     * @param page 分页的起始位置
     * @param limit 分页大小
     * @param name 查询的子条件
     * @return 返回一个 HashMap 给前端用于识别数据
     */
    @RequestMapping("/typeAll")
    @ResponseBody
    public HashMap<String,Object> typeAll( String page, String limit, String name ){
        HashMap<String, Object> resultMap = new HashMap<>();
        HashMap<String, Object> paramMap = new HashMap<>();

        int pageSize = Integer.parseInt( limit );
        int pageContext = ( Integer.parseInt( page ) - 1 ) * pageSize;

        if ( name == null || name.equals( "" ) ){
            name = "%%";
        } else {
            name = "%" + name + "%";
        }

        paramMap.put( "pageContext", pageContext );
        paramMap.put( "pageSize", pageSize );
        paramMap.put( "name", name );

        ArrayList<Type> resultList = typeService.getAllDataByName(paramMap);

        resultMap.put( "count", typeService.getAllDataContext( paramMap ) );
        resultMap.put( "data", resultList );
        resultMap.put( "code", 0 );
        return resultMap;
    }

    // 执行删除表数据
    @RequestMapping("/deleteType")
    @ResponseBody
    public HashMap<String,Object> deleteType( String ids ){
        HashMap<String, Object> resultMap = new HashMap<>();

        String[] split = ids.split(",");
        for ( String id : split ){
            typeService.deleteData( Integer.parseInt( id ) );
        }

        resultMap.put( "code", 0 );
        return resultMap;
    }

    // 跳转至修改表数据
    @RequestMapping("/queryTypeInfoById")
    public String queryTypeJump( String id, HttpServletRequest request, Model model ){

        Type type = typeService.queryTypeById( Integer.parseInt( id ) );

        request.getSession().setAttribute( Constant.TypeId, id );
        model.addAttribute( "info", type );

        return "type/updateType";
    }

    // 执行修改表数据
    @RequestMapping("/updateTypeSubmit")
    @ResponseBody
    public HashMap<String,Object> updateType( @RequestBody(required = false) Type type, HttpServletRequest request ){
        HashMap<String, Object> resultMap = new HashMap<>();
        HashMap<String, Object> paramMap = new HashMap<>();

        // 将存在 Session 中的值取出，并将其转换为整形
        String attribute = (String) request.getSession().getAttribute( Constant.TypeId );
        int typeId = Integer.parseInt( attribute );

        // 删除 Session 
        request.getSession().removeAttribute( Constant.TypeId );

        paramMap.put( "id", typeId );
        paramMap.put( "name", type.getName() );
        paramMap.put( "remarks", type.getRemarks() );

        boolean flag = typeService.updateData(paramMap);
        if ( flag ){
            resultMap.put( "code", 0 );
        }

        return resultMap;
    }

    // 跳转至添加表数据的页面
    @RequestMapping("/typeAdd")
    public String typeAddJump(){

        return "type/typeAdd";
    }

    // 提交增添表数据
    @RequestMapping("/addTypeSubmit")
    @ResponseBody
    public HashMap<String,Object> addTypeSubmit( String name, String remarks ){
        HashMap<String, Object> resultMap = new HashMap<>();
        HashMap<String, Object> paramMap = new HashMap<>();

        paramMap.put( "name", name );
        paramMap.put( "remarks", remarks );

        boolean flag = typeService.addData( paramMap );
        if ( flag ){
            resultMap.put( "code", 0 );
        }
        
        return resultMap;
    }
}
